//
//  ASN1AppleReceiptTest.swift
//  ASN1DecoderTests
//
//  Created by Filippo Maguolo on 08/12/2019.
//  Copyright © 2019 Filippo Maguolo. All rights reserved.
//

import XCTest
@testable import ASN1Decoder

class ASN1AppleReceiptTest: XCTestCase {

    func testReceipt() {
        guard let data = Data(base64Encoded: receiptSample) else {
            XCTFail("Invalid Base64 format")
            return
        }
        let pkcs7 = try? PKCS7(data: data)
        guard let receipt = pkcs7?.receipt() else {
            XCTFail("Invalid Receipt format")
            return
        }
        
        XCTAssertEqual(receipt.bundleIdentifier, "com.belive.app.ios")
        XCTAssertEqual(receipt.bundleVersion, "3")
        XCTAssertEqual(receipt.originalApplicationVersion, "1.0")
        XCTAssertEqual(receipt.receiptCreationDateString, "2018-11-13T16:46:31Z")
        XCTAssertEqual(receipt.sha1?.hexEncodedString(), "27384ED414759313951AB084A902E4C2F52E64FF")
        XCTAssertEqual(receipt.opaqueValue?.hexEncodedString(), "34E2349B0BD662E4EC127A6BFD10AF24")

        XCTAssertEqual(receipt.inAppPurchases?.first?.productId, "test2")
        XCTAssertEqual(receipt.inAppPurchases?.first?.quantity, 1)
        XCTAssertEqual(receipt.inAppPurchases?.first?.transactionId, "1000000472106082")

        XCTAssertNotNil(pkcs7?.signatures?.first?.signature)
    }
    
    let receiptSample = "MIITuAYJKoZIhvcNAQcCoIITqTCCE6UCAQExCzAJBgUrDgMCGgUAMIIDWQYJKoZIhvcNAQcBoIIDSgSCA0YxggNCMAoCAQgCAQEEAhYAMAoCARQCAQEEAgwAMAsCAQECAQEEAwIBADALAgEDAgEBBAMMATMwCwIBCwIBAQQDAgEAMAsCAQ4CAQEEAwIBWjALAgEPAgEBBAMCAQAwCwIBEAIBAQQDAgEAMAsCARkCAQEEAwIBAzAMAgEKAgEBBAQWAjQrMA0CAQ0CAQEEBQIDAYfPMA0CARMCAQEEBQwDMS4wMA4CAQkCAQEEBgIEUDI1MDAYAgEEAgECBBA04jSbC9Zi5OwSemv9EK8kMBsCAQACAQEEEww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